package com.hrms.repository;

import com.hrms.entity.OperationLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface OperationLogRepository extends JpaRepository<OperationLog, Long> {
    
    @Query("SELECT o FROM OperationLog o WHERE " +
           "(:userId IS NULL OR o.user.id = :userId) AND " +
           "(:operationType IS NULL OR o.operationType = :operationType) AND " +
           "(:startDate IS NULL OR o.createdAt >= :startDate) AND " +
           "(:endDate IS NULL OR o.createdAt <= :endDate)")
    Page<OperationLog> findByFilters(@Param("userId") Long userId,
                                     @Param("operationType") String operationType,
                                     @Param("startDate") LocalDateTime startDate,
                                     @Param("endDate") LocalDateTime endDate,
                                     Pageable pageable);
    
    List<OperationLog> findByUserIdOrderByCreatedAtDesc(Long userId);
}
